package com.skymobi.cassandra.service;

import java.util.HashMap;
import java.util.List;

import org.apache.cassandra.service.Cassandra;
import org.apache.cassandra.service.ConsistencyLevel;
import org.apache.cassandra.service.NotFoundException;
import org.apache.thrift.TException;

import com.skymobi.cassandra.model.Keyspace;

public class CassandraClientImpl implements CassandraClient {

	private final static String PROP_CLUSTER_NAME = "cluster name";
	private final static String PROP_CONFIG_FILE = "config file";
	private final static String PROP_TOKEN_MAP = "token map";
	private final static String PROP_KEYSPACE = "keyspaces";
	private final static String PROP_VERSION = "version";

	/** The thrift object */
	private final Cassandra.Client cassandra;

	// private List<String> keyspaces;

	private HashMap<String, Keyspace> keyspaceMap;

	private String clusterName;

	private String tokenMap;

	private String configFile;

	private String serverVersion;

	public CassandraClientImpl(Cassandra.Client cassandraThriftClient) {
		cassandra = cassandraThriftClient;
	}

	@Override
	public String getClusterName() throws TException {
		if (clusterName == null) {
			clusterName = getStringProperty(PROP_CLUSTER_NAME);
		}
		return clusterName;
	}

	@Override
	public String getConfigFile() throws TException {
		if (configFile == null) {
			configFile = getStringProperty(PROP_CONFIG_FILE);
		}
		return null;
	}

	@Override
	public Keyspace getKeySpace(String keySpaceName)
			throws IllegalArgumentException, NotFoundException, TException {
		// cassandra.
		return null;
	}

	@Override
	public Keyspace getKeySpace(String keySpaceName,
			ConsistencyLevel consistencyLevel) throws IllegalArgumentException,
			NotFoundException, TException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> getKeyspaces() throws TException {
		return cassandra.get_string_list_property(PROP_KEYSPACE);
	}

	@Override
	public String getStringProperty(String propertyName) throws TException {
		return cassandra.get_string_property(propertyName);
	}

	@Override
	public String getTokenMap() throws TException {
		if (tokenMap == null) {
			tokenMap = getStringProperty(PROP_TOKEN_MAP);
		}
		return tokenMap;
	}

	public String getServerVersion() throws TException {
		if (serverVersion == null) {
			serverVersion = getStringProperty(PROP_VERSION);
		}
		return serverVersion;
	}
}
